﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Konst3d.Efficiency.Suite;
using Konst3d.Efficiency.Suite.SparsedBinary;

namespace Dev1
{
    class Program
    {
        /*
        static EfficiencySuite _suite;
        static SparseBinaryStorage _storage;
        */

        private static string GetPathWithoutExt(string path)
        {
            return System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));
        }

        static void Main(string[] args)
        {
            /*
            // create
            _storage = new SparseBinaryStorage(@"d:\pem_storages\storage1");
            _storage.Create();

            // _storage.Delete();
            
            _suite = new EfficiencySuite(_storage);

            LabelsStorage labels = LabelsStorage.Create(@"d:\pem_storages\stat1", true);

            StatisticStorage statistic = StatisticStorage.Create(@"d:\pem_storages\stat1.statistic", true, labels);

            statistic.SetDay(DateTime.Now, true, 12.34f, "ggg");
            statistic.SetDay(DateTime.Now.AddDays(1), false, 56.78f, "555");
            statistic.SetDay(DateTime.Now.AddDays(2), true, 1.7f, "59995");
            statistic.SetDay(DateTime.Now.AddDays(3), false, 88.45f, "77");

            Konst3d.Efficiency.Storage.StatisticDay day1 = statistic.GetDay(DateTime.Now);
            Konst3d.Efficiency.Storage.StatisticDay day2 = statistic.GetDay(DateTime.Now.AddDays(1));
            Konst3d.Efficiency.Storage.StatisticDay day3 = statistic.GetDay(DateTime.Now.AddDays(2));
            Konst3d.Efficiency.Storage.StatisticDay day4 = statistic.GetDay(DateTime.Now.AddDays(3));

            Konst3d.Efficiency.Storage.StatisticDay[] range = statistic.GetRange(DateTime.Now, DateTime.Now.AddDays(3));

            statistic.SetDay(DateTime.Now,              false, 11.11f, "A");
            statistic.SetDay(DateTime.Now.AddDays(1),   true,  22.22f, "B");
            statistic.SetDay(DateTime.Now.AddDays(2),   false, 33.33f, "C");
            statistic.SetDay(DateTime.Now.AddDays(3),   true,  44.44f, "D");

            statistic.SetDayActive(DateTime.Now.AddDays(2),     true);
            statistic.SetDayCommentary(DateTime.Now.AddDays(2), "ZZZ-XXX-WWW");
            statistic.SetDayValue(DateTime.Now.AddDays(2),      999.999f);

            day1 = statistic.GetDay(DateTime.Now);
            day2 = statistic.GetDay(DateTime.Now.AddDays(1));
            day3 = statistic.GetDay(DateTime.Now.AddDays(2));
            day4 = statistic.GetDay(DateTime.Now.AddDays(3));

            range = statistic.GetRange(DateTime.Now, DateTime.Now.AddDays(3));
            */

            /*
            LabelsStorage labels = new LabelsStorage(@"d:\pem_storages\labels\test", true);

            labels.AddLabel("1 one");
            labels.AddLabel("2 two");
            labels.AddLabel("long0 long1 long2 long3 long4 long5 long6 long7 long8 long9 long9 long10 long11 long12 long13");

            string s1 = labels.GetLabel(0);
            string s2 = labels.GetLabel(1);
            string s3 = labels.GetLabel(2);

            labels.DeleteLabel(2);
            labels.DeleteLabel(1);
            labels.DeleteLabel(0);

            labels.AddLabel("11");
            labels.AddLabel("22");
            labels.AddLabel("-xxxxxxx-");

            s1 = labels.GetLabel(0);
            s2 = labels.GetLabel(1);
            s3 = labels.GetLabel(2);

            int id1 = labels.FindLabel("11");
            int id2 = labels.FindLabel("22");
            int id3 = labels.FindLabel("33");
            int id4 = labels.FindLabel("-xxxxxxx-");
            */

            /* MONITORING FILE
            MonitorFile monFile = MonitorFile.Create(@"d:\pem_storages\storage1\mon1.monitor", true); // MonitorFile.Load(@"d:\pem_storages\storage1\mon1.monitor"); // 

            //monFile.TypeID = 12;
            //monFile.NameID = 13;
            //monFile.DescriptionID = 14;

            monFile.Dispose();
            */

            /*
            MonitoringExceptionsStorage exceptions = MonitoringExceptionsStorage.Load(@"d:\pem_storages\exc.exceptions"); // MonitoringExceptionsStorage.Create(@"d:\pem_storages\exc.exceptions", true);  // 

            //exceptions.UpdateDayOfWeek(0, true);
            //exceptions.UpdateDayOfWeek(1, false);
            //exceptions.UpdateDayOfWeek(2, true);
            //exceptions.UpdateDayOfWeek(3, false);
            //exceptions.UpdateDayOfWeek(4, true);
            //exceptions.UpdateDayOfWeek(5, false);
            //exceptions.UpdateDayOfWeek(6, true);

            bool[] week = exceptions.GetDaysOfWeek();

            //exceptions.UpdateDayOfMonth( 0, true);
            //exceptions.UpdateDayOfMonth(15, true);
            //exceptions.UpdateDayOfMonth(29, true);
            //exceptions.UpdateDayOfMonth(30, true);

            bool[] month = exceptions.GetDaysOfMonth();
            */

            // add programms
            //_suite.Monitors.Add("Ice", "Coding of mine ...");
            //_suite.Monitors.Add("Диета", "Соблюдать...");

            // Console.WriteLine(int.MaxValue);

            /*
            Console.WriteLine("Contains (true): " + _suite.Monitors.Contains("Ice"));
            Console.WriteLine("Contains (false): " + _suite.Monitors.Contains("111"));

            foreach (var monitor in _suite.Monitors)
            {
                Console.WriteLine(monitor.Name);
            }

            // add day
            _suite.Monitors["Ice"].Statistic.Today.Set(12, true, "...");

            _suite.Monitors["Ice"].Statistic[DateTime.Now].Value = 32;
            _suite.Monitors["Ice"].Statistic[DateTime.Now].Active = true;
            _suite.Monitors["Ice"].Statistic[DateTime.Now].Commentary = "...";
            _suite.Monitors["Ice"].Statistic[DateTime.Now].Set(12.34f, true, "...");

            _suite.Monitors["Ice"].Statistic[DateTime.Now.AddDays(-1)].Set(12.34f, true, "..."); // yesterday
            _suite.Monitors["Ice"].Statistic[DateTime.Now.AddDays(1)].Set(12.34f, true, "..."); // tomorrow
            */

            /*
            for (int i = 0; i <= 100; ++i)
            {
                DateTime date = new DateTime(2000 + i, 1, 1);

                Console.WriteLine(date.Year + " : " + DateTime.IsLeapYear(date.Year));
            }
            */

            /*
            using (MonitorStorage store = MonitorStorage.Create(@"d:\pem_storages\mon1", true))
            {
                store.Active = true;
                store.Name = "Name";
                store.Description = "Description";
                store.ID = 124;

                //
                // MonitorFile test
                //

                bool active = store.Active;
                string name = store.Name;
                string descr = store.Description;
                uint id = store.ID;

                store.Name = "name_2";
                store.Description = "descr_2";

                //
                // Exceptions test
                //

                store.Exceptions.UpdateDayOfWeek(0, true);
                store.Exceptions.UpdateDayOfWeek(1, false);
                store.Exceptions.UpdateDayOfWeek(2, true);
                store.Exceptions.UpdateDayOfWeek(3, false);
                store.Exceptions.UpdateDayOfWeek(4, true);
                store.Exceptions.UpdateDayOfWeek(5, false);
                store.Exceptions.UpdateDayOfWeek(6, true);

                bool[] week = store.Exceptions.GetDaysOfWeek();

                for (int i = 0; i < 31; ++i)
                {
                    store.Exceptions.UpdateDayOfMonth(i, i % 2 > 0);
                }

                bool[] month = store.Exceptions.GetDaysOfMonth();

                //
                // statistics
                //

                store.Statistic.SetDay(DateTime.Now,              true,  11.11f, "11.11");
                store.Statistic.SetDay(DateTime.Now.AddDays(1),   false, 22.22f, "22.22");
                store.Statistic.SetDay(DateTime.Now.AddYears(1),  false, 33.33f, "33.33");
                store.Statistic.SetDay(DateTime.Now.AddYears(2),  false, 44.44f, "44.44");
                store.Statistic.SetDay(DateTime.Now.AddYears(99), false, 99.99f, "99.99");
            }

            using (MonitorStorage store2 = MonitorStorage.Load(@"d:\pem_storages\mon1"))
            {
                bool active = store2.Active;
                string name = store2.Name;
                string descr = store2.Description;
                uint id = store2.ID;

                bool[] week = store2.Exceptions.GetDaysOfWeek();
                bool[] month = store2.Exceptions.GetDaysOfMonth();

                Konst3d.Efficiency.Storage.StatisticDay day1 = store2.Statistic.GetDay(DateTime.Now);
                Konst3d.Efficiency.Storage.StatisticDay day2 = store2.Statistic.GetDay(DateTime.Now.AddDays(1));
                Konst3d.Efficiency.Storage.StatisticDay day3 = store2.Statistic.GetDay(DateTime.Now.AddYears(1));
                Konst3d.Efficiency.Storage.StatisticDay day4 = store2.Statistic.GetDay(DateTime.Now.AddYears(2));
                Konst3d.Efficiency.Storage.StatisticDay day5 = store2.Statistic.GetDay(DateTime.Now.AddYears(99));

                store2.Delete();
            }

            string path = "c:\\folder\\subfolder\\file.ext";

            Console.WriteLine(Path.Combine( Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) ));
            */

            // Konst3d.Efficiency.Storage.SparseBinaryStorage.MonitorCollection collection = Konst3d.Efficiency.Storage.SparseBinaryStorage.MonitorCollection.Create(@"d:\pem_storages\test_mons");

            /*
            SparseBinaryStorage storage = SparseBinaryStorage.Create(@"d:\pem_storages\storage13", true); // SparseBinaryStorage.Load(@"d:\pem_storages\storage13"); // 
            EfficiencySuite suite = new EfficiencySuite(storage);

            int id1 = suite.Monitors.Add();
            int id2 = suite.Monitors.Add();
            int id3 = suite.Monitors.Add();

            suite.Monitors[id1].Name = "name 1";
            suite.Monitors[id1].Description = "decr 1";

            suite.Monitors[id2].Name = "name 2";
            suite.Monitors[id2].Description = "decr 2";

            suite.Monitors[id3].Name = "name 3";
            suite.Monitors[id3].Description = "decr 3";

            suite.Dispose();
            storage.Dispose();

            storage = SparseBinaryStorage.Load(@"d:\pem_storages\storage13");

            string s1 = suite.Monitors[id1].Description;
            string s2 = suite.Monitors[id2].Description;
            string s3 = suite.Monitors[id3].Description;

            DateTime d1 = suite.Monitors[id1].Created;
            DateTime d2 = suite.Monitors[id2].Created;
            DateTime d3 = suite.Monitors[id3].Created;

            storage.Delete();
            */

            /*
            collection.Add("mon1", "descr 1");
            collection.Add("mon2", "descr 2");
            collection.Add("mon3", "descr 3");

            collection["mon1"].Description = "_descr_ 11";
            collection["mon2"].Description = "_descr_ 22";
            collection["mon3"].Description = "_descr_ 33";

            //

            string s1 = collection["mon1"].Description;
            string s2 = collection["mon2"].Description;
            string s3 = collection["mon3"].Description;

            collection.Delete();
            */

            /*
            Konst3d.Efficiency.Suite.MonitorCollection coll = new Konst3d.Efficiency.Suite.MonitorCollection(collection);

            coll.Add("mon1", "descr 1");
            coll.Add("mon2", "descr 2");
            coll.Add("mon3", "descr 3");

            coll["mon1"].Active = true;
            coll["mon1"].Description = "descr 1";
            coll["mon1"].Exceptions.DaysOfWeek[0] = true;
            coll["mon1"].Exceptions.DaysOfWeek[1] = true;
            coll["mon1"].Exceptions.DaysOfMonth[11] = true;
            coll["mon1"].Exceptions.DaysOfMonth[12] = true;

            coll["mon2"].Active = true;
            coll["mon2"].Description = "descr 2";
            coll["mon2"].Exceptions.DaysOfWeek[5] = true;
            coll["mon2"].Exceptions.DaysOfWeek[6] = true;
            coll["mon2"].Exceptions.DaysOfMonth[5] = true;
            coll["mon2"].Exceptions.DaysOfMonth[6] = true;
            */

            /*
            StorageFile fle = StorageFile.Create(@"d:\pem_storages\store.fle", true, "login", "pass");

            fle.Login = "__login__";
            fle.Password = "__pass__";

            fle.Dispose();

            bool protection = StorageFile.IsPasswordProtected(@"d:\pem_storages\store.fle");

            fle = StorageFile.Open(@"d:\pem_storages\store.fle", "__login__", "__pass__");

            fle.Dispose();
            */
            /*
            Console.WriteLine(DateTime.Now.DayOfWeek + " : " + (double)DateTime.Now.DayOfWeek);

            double year = 1.0 / 365.0;
            double year10 = 1.0 / (365.0 * 10);
            double year100 = 1.0 / (365.0 * 100);

            double efYear = year * 365.0;
            double efYear10 = year10 * 365.0 * 10.0;
            double efYear100 = year100 * 365.0 * 100.0;

            Console.WriteLine(year);
            Console.WriteLine(year10);
            Console.WriteLine(year100);
            Console.WriteLine(efYear);
            Console.WriteLine(efYear10);
            Console.WriteLine(efYear100);

            double result = 0;
            int counter = 0;
            int countDays = 100 * 365 * 100; // 100 monitors, 100 years each...

            Console.WriteLine("Press any key to start long computation...");
            Console.ReadKey();

            DateTime start = DateTime.Now;

            for (int i = 0; i < countDays; ++i)
            {
                result += i;
                counter += 1;
            }

            Console.WriteLine("Done. " + (DateTime.Now - start).TotalMilliseconds);
            */

            /*
            DateTime date = new DateTime(-1, 0, 0);

            Console.WriteLine((int)DateTime.Now.DayOfWeek);
            Console.WriteLine(date);
            */

            /*
            MilestoneStorage storage = MilestoneStorage.Create(@"d:\pem_storages\miles\milestone1", true);

            int m1 = storage.Add(DateTime.Now, "title 1", "descr 1");
            int m2 = storage.Add(DateTime.Now.AddDays(1), "title 2", "descr 2");
            int m3 = storage.Add(DateTime.Now.AddDays(2), "title 3", "descr 3");
            int m4 = storage.Add(DateTime.Now.AddDays(3), "title 4", "descr 4");

            storage[m1].Title = "title 11 (edited)";
            storage[m1].Description = "descr 11 (edited)";

            storage.Remove(m2);

            m2 = storage.Add(DateTime.Now.AddDays(1), "new title", "new descr");

            List<IMilestone> list = new List<IMilestone>();

            foreach (var milestone in storage)
            {
                list.Add(milestone);
            }

            storage.Add(DateTime.Now, "title now 1", "descr now 1");
            storage.Add(DateTime.Now, "title now 2", "descr now 2");
            storage.Add(DateTime.Now, "title now 3", "descr now 3");

            var mile1 = storage.GetMilestones(DateTime.Now);
            */

            /*
            TaskStorage storage = TaskStorage.Create(@"d:\pem_storages\tasks\tasks", true);

            // add
            int id1 = storage.Add(DateTime.Now, DateTime.Now.AddDays(1), TaskSeverity.Low, "Title 1", "Descr 1");
            int id2 = storage.Add(DateTime.Now, DateTime.Now.AddDays(2), TaskSeverity.Normal, "Title 2", "Descr 2");
            int id3 = storage.Add(DateTime.Now, DateTime.Now.AddDays(3), TaskSeverity.Hight, "Title 3", "Descr 3");
            int id4 = storage.Add(DateTime.Now, DateTime.Now.AddDays(4), TaskSeverity.Critical, "Title 4", "Descr 4");

            // get
            var t1 = storage[id1];
            var t2 = storage[id2];
            var t3 = storage[id3];
            var t4 = storage[id4];

            // edit
            storage[id1].DeadLine = DateTime.Now.AddDays(10);
            storage[id1].Severity = TaskSeverity.Critical;
            storage[id1].State    = TaskState.Completed;
            storage[id1].Title += " (completed)";
            storage[id1].Description += " (completed)";

            // remove
            storage.Remove(id2);
            */

            /*
            TaskStorage storage = TaskStorage.Open(@"d:\pem_storages\tasks\tasks");

            var active    = storage.Get(TaskState.Active);
            var completed = storage.Get(TaskState.Completed);
            var failed    = storage.Get(TaskState.Failed);
            */

            /*
            var file = MonitoringGroupFile.Create(@"d:\pem_storages\mgroup1.group", true);

            int id = file.ID;
            DateTime created = file.Created;
            string title = file.Title;
            string description = file.Description;

            file.ID = 123;
            file.Created = new DateTime(1234, 12, 1);
            file.Title = "TITLE";
            file.Description = "DESCR";

            file.Dispose();

            file = MonitoringGroupFile.Open(@"d:\pem_storages\mgroup1.group");

            id = file.ID;
            created = file.Created;
            title = file.Title;
            description = file.Description;

            file.Delete();
            */
            /*
            BinaryWriter writer = new BinaryWriter(File.Create(@"d:\pem_storages\binary.bin", 1024, FileOptions.RandomAccess));

            writer.Write(120);
            writer.Write(121);
            writer.Write(122);
            writer.Write(123);
            writer.Write(124);

            writer.Write(1120,  0);
            writer.Write(1121,  4);
            writer.Write(1122,  8);
            writer.Write(1123, 12);
            writer.Write(1124, 16);

            writer.Close();

            BinaryReader reader = new BinaryReader(File.Open(@"d:\pem_storages\binary.bin", FileMode.Open));

            int n5 = reader.ReadInt32(16);
            int n4 = reader.ReadInt32(12);
            int n3 = reader.ReadInt32(8);
            int n2 = reader.ReadInt32(4);
            int n1 = reader.ReadInt32(0);
            */
            /*
            ListFile<int> file = ListFile<int>.Create(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist", true);

            file.Add(11);
            file.Add(22);
            file.Add(33);
            file.Add(44);
            file.Add(55);

            List<int> list = new List<int>();

            foreach (var item in file)
            {
                list.Add(item);
            }

            //

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Remove(33);
            list.Clear();

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            file.Add(33);

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            file.Remove(11);
            file.Remove(55);

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            file.Remove(11);
            file.Remove(55);

            foreach (var item in file)
            {
                list.Add(item);
            }

            file.Insert(2, 555);
            file.Insert(0, 111);

            file.Dispose();
            list.Clear();

            file = ListFile<int>.Open(new ListFileLayoutInt(), @"d:\pem_storages\test.intlist");

            foreach (var item in file)
            {
                list.Add(item);
            }
            */

            /*
            var storage = BuyCollectionStorage.Create(@"d:\pem_storages\buys.buys", true);

            int id0 = storage.Add("title0", "descr0", 10, 100, DateTime.Now, DateTime.Now.AddDays(1));
            int id1 = storage.Add("title1", "descr1", 11, 110, DateTime.Now, DateTime.Now.AddDays(2));
            int id2 = storage.Add("title2", "descr2", 12, 120, DateTime.Now, DateTime.Now.AddDays(3));

            storage.Dispose();

            storage = BuyCollectionStorage.Open(@"d:\pem_storages\buys.buys");
            */

            /*
            var file = NoteFile.Create(@"d:\pem_storages\notes.notes", true);

            int id1 = file.AddEntry(11, 12, DateTime.Now);
            int id2 = file.AddEntry(22, 23, DateTime.Now);
            int id3 = file.AddEntry(33, 34, DateTime.Now);

            var entry1 = file[id1];
            var entry2 = file[id2];
            var entry3 = file[id3];

            file.RemoveEntry(id2);

            int id4 = file.AddEntry(44, 45, DateTime.Now);

            file.Dispose();

            file = NoteFile.Open(@"d:\pem_storages\notes.notes");

            var entries = file.GetAll();
            */

            /*
            var storage = NoteCollectionStorage.Create(@"d:\pem_storages\notes\notes", true);

            storage.Add("title1", "text1");
            storage.Add("title2", "text2");
            storage.Add("title3", "text3");

            storage.Dispose();

            storage = NoteCollectionStorage.Open(@"d:\pem_storages\notes\notes");

            storage.Remove(1);

            storage.Dispose();

            storage = NoteCollectionStorage.Open(@"d:\pem_storages\notes\notes");
            */

            /*
            writer.Flush();
            writer.Seek(0, SeekOrigin.Begin);
            stream.Seek(0, SeekOrigin.Begin); // TOFIX: does it really needed?
            */

            FileStream stream = File.Open(@"d:\pem_storages\test.tst", FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
            BinaryReader reader = new BinaryReader(stream, Encoding.UTF8);
            BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);

            writer.Write(123);
            writer.Write(888);
            writer.Flush();

            long pos0 = stream.Position;

            writer.Seek(0, SeekOrigin.Begin);

            long pos = stream.Position;

            Console.ReadKey();
        }
    }
}
